function J = cost(A, Y, W, lambda)
% 计算均方误差
% input:
%     A - matrix
%     Y - matrix
%     W - matrix cell array
%     lambda - scalar
% output:
%     J - scalar
    m = size(Y, 1);
    d = A - Y;
    J = 1 / (2 * m) * d(:)' * d(:);

    % 正则项
    r = 0;
    for k = 1: length(W)
        w = W{k};
        r = r + w(:)' * w(:);
    end
    r = lambda * r / (2 * m);

    % 正则化误差
    J = J + r;
end